import java.util.Scanner;

public class Manager {
    private final Scanner scanner;
    private final Film[] films;
    private final Session[] sessions;
    private final User[] users;

    public Manager(Film[] films, Session[] sessions, User[] users) {
        this.scanner = new Scanner(System.in);
        this.films = films;
        this.sessions = sessions;
        this.users = users;
    }

    public void manageFilms() {
        while (true) {
            System.out.println("=== 经理-影片管理 ===");
            System.out.println("1-列出影片 2-添加影片 3-修改影片 4-删除影片 5-查询影片 6-返回");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    listFilms();
                    break;
                case 2:
                    addFilm();
                    break;
                case 3:
                    modifyFilm();
                    break;
                case 4:
                    deleteFilm();
                    break;
                case 5:
                    queryFilm();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    public void manageSessions() {
        while (true) {
            System.out.println("=== 经理-排片管理 ===");
            System.out.println("1-增加场次 2-修改场次 3-删除场次 4-列出场次 5-自动排片 6-返回");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    addSession();
                    break;
                case 2:
                    modifySession();
                    break;
                case 3:
                    deleteSession();
                    break;
                case 4:
                    listSessions();
                    break;
                case 5:
                    autoArrangeSessions();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void listFilms() {
        System.out.println("=== 影片列表 ===");
        System.out.println("片名\t导演\t主演\t时长\t评分");
        for (Film film : films) {
            if (film != null) {
                System.out.println(film.getName() + "\t" + film.getDirector() + "\t" + film.getActor() + "\t" + film.getDuration() + "\t" + film.getScore());
            }
        }
    }

    private void addFilm() {
        System.out.println("请输入片名：");
        String filmName = scanner.nextLine();
        System.out.println("请输入导演：");
        String director = scanner.nextLine();
        System.out.println("请输入主演：");
        String actor = scanner.nextLine();
        System.out.println("请输入剧情简介：");
        String plot = scanner.nextLine();
        System.out.println("请输入时长：");
        String duration = scanner.nextLine();
        System.out.println("请输入评分：");
        double score = scanner.nextDouble();
        scanner.nextLine();
        for (int i = 0; i < films.length; i++) {
            if (films[i] == null) {
                films[i] = new Film(filmName, director, actor, plot, duration, score);
                System.out.println("影片添加成功！");
                break;
            }
        }
    }

    private void modifyFilm() {
        System.out.println("请输入要修改的影片名：");
        String oldFilmName = scanner.nextLine();
        for (Film film : films) {
            if (film != null && film.getName().equals(oldFilmName)) {
                System.out.println("请输入新导演：");
                String newDirector = scanner.nextLine();
                film.director = newDirector;
                System.out.println("信息修改成功！");
                break;
            }
        }
    }

    private void deleteFilm() {
        System.out.println("请输入要删除的影片名：");
        String delFilmName = scanner.nextLine();
        for (int i = 0; i < films.length; i++) {
            if (films[i] != null && films[i].getName().equals(delFilmName)) {
                films[i] = null;
                System.out.println("影片删除成功！");
                break;
            }
        }
    }

    private void queryFilm() {
        System.out.println("请输入要查询的影片名/导演/主演：");
        String queryFilm = scanner.nextLine();
        for (Film film : films) {
            if (film != null && (film.getName().contains(queryFilm) || film.getDirector().contains(queryFilm) || film.getActor().contains(queryFilm))) {
                System.out.println("片名：" + film.getName() + " 导演：" + film.getDirector() + " 主演：" + film.getActor());
            }
        }
    }

    private void listSessions() {
        System.out.println("=== 场次列表 ===");
        System.out.println("场次 ID\t影片名\t放映时间\t放映厅\t价格");
        for (Session session : sessions) {
            if (session != null) {
                System.out.println(session.getId() + "\t" + session.getFilmName() + "\t" + session.getTime() + "\t" + session.getHall() + "\t" + session.getPrice());
            }
        }
    }

    private void addSession() {
        System.out.println("请输入场次 ID：");
        String sessionId = scanner.nextLine();
        System.out.println("请输入影片名：");
        String filmName = scanner.nextLine();
        System.out.println("请输入放映时间：");
        String time = scanner.nextLine();
        System.out.println("请输入放映厅：");
        String hall = scanner.nextLine();
        System.out.println("请输入价格：");
        String price = scanner.nextLine();
        System.out.println("请输入放映厅行数：");
        int row = scanner.nextInt();
        System.out.println("请输入放映厅列数：");
        int col = scanner.nextInt();
        scanner.nextLine();
        for (int i = 0; i < sessions.length; i++) {
            if (sessions[i] == null) {
                sessions[i] = new Session(sessionId, filmName, time, hall, price, row, col);
                System.out.println("场次添加成功！");
                break;
            }
        }
    }

    private void modifySession() {
        System.out.println("请输入要修改的场次 ID：");
        String modSessionId = scanner.nextLine();
        for (Session session : sessions) {
            if (session != null && session.getId().equals(modSessionId)) {
                System.out.println("请输入新影片名（空则不修改）：");
                String newFilmName = scanner.nextLine();
                if (!newFilmName.isEmpty()) {
                    session.filmName = newFilmName;
                }
                System.out.println("请输入新放映时间（空则不修改）：");
                String newTime = scanner.nextLine();
                if (!newTime.isEmpty()) {
                    session.time = newTime;
                }
                System.out.println("请输入新放映厅（空则不修改）：");
                String newHall = scanner.nextLine();
                if (!newHall.isEmpty()) {
                    session.hall = newHall;
                }
                System.out.println("请输入新价格（空则不修改）：");
                String newPrice = scanner.nextLine();
                if (!newPrice.isEmpty()) {
                    session.price = newPrice;
                }
                System.out.println("场次信息修改成功！");
                break;
            }
        }
    }

    private void deleteSession() {
        System.out.println("请输入要删除的场次 ID：");
        String delSessionId = scanner.nextLine();
        for (int i = 0; i < sessions.length; i++) {
            if (sessions[i] != null && sessions[i].getId().equals(delSessionId)) {
                boolean hasSold = false;
                for (int[] rowArr : sessions[i].seats) {
                    for (int seat : rowArr) {
                        if (seat == 1) {
                            hasSold = true;
                            break;
                        }
                    }
                    if (hasSold) {
                        break;
                    }
                }
                if (hasSold) {
                    System.out.println("该场次已有售出座位，无法删除！");
                } else {
                    sessions[i] = null;
                    System.out.println("场次删除成功！");
                }
                break;
            }
        }
    }

    private void autoArrangeSessions() {
        System.out.println("=== 自动排片（按评分高优先安排黄金时段） ===");
        String goldenTime = "2025-01-04 19:00";
        for (Film film : films) {
            if (film != null && film.getScore() >= 8.0) {
                for (int i = 0; i < sessions.length; i++) {
                    if (sessions[i] == null) {
                        sessions[i] = new Session("AUTO" + (i + 1), film.getName(), goldenTime, "黄金厅", "60", 8, 10);
                        System.out.println("为影片《" + film.getName() + "》自动安排黄金时段场次：" + sessions[i].getId());
                        break;
                    }
                }
            }
        }
        System.out.println("自动排片完成！");
    }
}